<template>
  <!--轮播图-->
  <el-carousel height="450px">
    <el-carousel-item v-for="(banner, index) in banners" :key="index">
      <el-image :src="banner"/>
    </el-carousel-item>
  </el-carousel>

  <!--广告位-->
  <div class="ad">
    <ul>
      <li><el-image src="/src/assets/banner/hz.jpg"/></li>
      <li><el-image src="/src/assets/banner/hz.jpg"/></li>
      <li><el-image src="/src/assets/banner/hz.jpg"/></li>
      <li><el-image src="/src/assets/banner/hz.jpg"/></li>
    </ul>
  </div>

  <!--推荐的上架商品-->
  <div class="recom" v-for="(category, index) in parentList" :key="index">
    <div class="categoryName">{{ category.name }}</div>
    <div class="goodsList">
      <ul>
        <li v-for="(goods, index) in category.goodsList" :key="index" @click="toGoodsView(goods.id)">
          <div class="pic">
            <el-image
                :src="`${SERVER_ADDR}/goods/pic/${goods.picList[0].url}`"
                style="width: 225px; height: 150px"
            />
          </div>
          <div class="name">{{ goods.name }}</div>
          <div class="dscp">{{ goods.dscp}}</div>
          <div class="price">￥ {{goods.price}}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup>
  import {ref} from "vue";
  import categoryApi from "@/api/categoryApi.js";
  import {useRouter} from  "vue-router"

  const router = useRouter();
  //服务器的地址
  const SERVER_ADDR = ref(import.meta.env.VITE_SERVER_ADDR)
  const banners = ref([
      "/src/assets/banner/banner1.jpg",
      "/src/assets/banner/banner2.jpg",
      "/src/assets/banner/banner3.jpg",
      "/src/assets/banner/banner4.jpg",
      "/src/assets/banner/banner5.jpg",
  ])
  const parentList = ref([]);
  //1.获取父分类 2.获取上架的
  const getParent = ()=>{
    const condition = {
      parentId: 0,
      status: 1
    };
    categoryApi.selectByPage(condition)
        .then(resp => {
          parentList.value = resp.data;
        })
  }
  //跳转到商品详情页面
  const toGoodsView = (id)=>{
    router.push({
      path: '/user/goods',
      query: {
        id
      }
    });

  }

  getParent();
</script>

<style scoped>
  .ad {
    margin: 20px 0;
  }
  .ad ul li {
    float: left;
    width: 285px;
    margin-right: 20px;
  }
  .ad ul li:last-child{
    margin-right: 0px;
  }
  .recom .goodsList ul li {
    width: 224px;
    float: left;
    margin-right: 20px;
    margin-bottom: 15px;
    text-align: center;
    background-color: #fff;
    cursor: pointer;
  }
  .recom .goodsList ul li:hover{
    box-shadow: 0px 0px 20px #000;
  }
  .recom .goodsList ul li:nth-child(5n) {
    margin-right: 0px;
  }

  .recom {
    margin-bottom: 20px;
  }

  .recom .categoryName {
    font-size: 20px;
    margin-bottom: 5px;
    font-weight: bold;
  }
  .name, .dscp, .price{
    line-height: 25px;
  }
  .recom .name {
    font-size: 16px;
    font-weight: bold;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .recom .dscp {
    color: #aaa;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .recom .price {
    font-size: 12px;
    color:  var(--theme-color);
  }
</style>